# #!/bin/bash

# CUR_DIR="$( cd "$( dirname "$(readlink -f "$0")" )" && pwd )"
# cd $CUR_DIR

# source ../../cluster_IP.conf || exit -1

# ORDERED_NUM=${#FABRIC_ORDERER_IPs[@]}
# ORG_NUM=${#FABRIC_PEER_IPs[@]}
# TARGET_HOST_FABRIC_SDK_DIR="~/deploy-bcs/fabric-sdk"

# function usage() {
#     echo "Usage:"
#     echo "    ./deploy_fabric_sdk.sh [server|down|bench]"
#     echo "        server: start a fabric sdk server"
#     echo "        down: down fabric sdk server"
#     echo "        bench: run fabric sdk bench"
# }

# function down_fabric_sdk_server() {
#     echo "Down fabric sdk server ..."
#     cd $CUR_DIR

#     for node in ${FABRIC_SDK_IPs[@]}; do
#         echo "==== down fabric-sdk on $node ===="
#         ssh ${HOST_USER}@${node} "
#             ps -ef | grep fabric-sdk | grep -v grep | awk '{print \$2}' | xargs kill -9 > /dev/null 2>&1
#         " &
#     done
#     wait

#     echo "Down fabric sdk server ... done"
# }

# function run_fabric_sdk_server() {
#     echo "Run fabric sdk server ..."

#     cd $CUR_DIR
    
#     down_fabric_sdk_server

#     local run_sdk_as_server="-s"

#     # generate config yaml for fabric sdk
#     for ((i=0; i<${ORG_NUM}; i++)); do
#         echo "==== generate config yaml for org${i} ===="
#         local port=$((i+8080))
#         python3 ./gen_config_yaml/gen_sdk_config.py -o $ORDERED_NUM -p $ORG_NUM -i $((i+1))
#     done

#     # make fabric sdk
#     cd ./fabric-sdk
#     go build .

#     # distribute fabric-sdk to all nodes & config hosts
#     cd $CUR_DIR
#     local org_id=1
#     for node in ${FABRIC_SDK_IPs[@]}; do
#         echo "==== send fabric-sdk binary & config to $node ===="
#         ssh ${HOST_USER}@${node} "
#             [ -d ${TARGET_HOST_FABRIC_SDK_DIR} ] && \
#                 rm -rf ${TARGET_HOST_FABRIC_SDK_DIR}/* || \
#                 mkdir -p ${TARGET_HOST_FABRIC_SDK_DIR}
#             mkdir -p ${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin
#         " && \
#         scp ./hosts_config.sh ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
#         scp ../../cluster_IP.conf ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
#         scp ./gen_config_yaml/sdk-config-${org_id}.yaml ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
#         scp ./fabric-sdk/fabric-sdk ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin/ > /dev/null 2>&1 && \
#         scp -r ./crypto-config ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 &
#         org_id=$((org_id+1))
#     done
#     wait

#     # run fabric sdk
#     local org_id=1
#     for node in ${FABRIC_SDK_IPs[@]}; do
#         echo "==== run fabric-sdk on $node ===="
#         local KAFAKA_IP=${KAFKA_IPs[$((org_id-1))]}
#         local KAFKA_PORT=$(($KAFKA_PORT_BASE+$org_id-1))
#         # set -x
#         ssh ${HOST_USER}@${node} "
#             bash ${TARGET_HOST_FABRIC_SDK_DIR}/hosts_config.sh
#             cd ${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin
#             ./fabric-sdk -o $org_id $run_sdk_as_server \
#                 -c ../sdk-config-${org_id}.yaml \
#                 -b ${KAFAKA_IP}:${KAFKA_PORT} \
#                 > ./fabric-sdk-org${org_id}.log 2>&1 &
#         " &
#         # set +x
#         org_id=$((org_id+1))
#     done
#     wait

#     echo "Run fabric sdk server ... done"
# }

# run_fabric_sdk_bench() {
#     echo "Run fabric sdk bench ..."

#     cd $CUR_DIR/fabric-network/deploy/fabric-sdk
#     # run fabric sdk bench
#     ./fabric-sdk -o 1 -b ${KAFKA_IPs[0]}:${KAFKA_PORT_BASE} -t ${KAFKA_TOPIC}  -c ../gen_config_yaml/sdk-config-1.yaml

#     echo "Run fabric sdk bench ... done"
# }

# if [ $# -ne 1 ]; then
#     usage
#     exit -1
# fi

# if [ $1 == "server" ]; then
#     run_fabric_sdk_server
# elif [ $1 == "bench" ]; then
#     run_fabric_sdk_bench
# elif [ $1 == "down" ]; then
#     down_fabric_sdk_server
# else
#     usage
# fi

#!/bin/bash

CUR_DIR="$( cd "$( dirname "$(readlink -f "$0")" )" && pwd )"
cd $CUR_DIR

source /etc/profile
source ../../cluster_IP.conf || exit -1

ORDERED_NUM=${#FABRIC_ORDERER_IPs[@]}
ORG_NUM=${#FABRIC_PEER_IPs[@]}
TARGET_HOST_FABRIC_SDK_DIR="~/deploy-bcs/fabric-sdk"

function usage() {
    echo "Usage:"
    echo "    ./deploy_fabric_sdk.sh [server|down|bench]"
    echo "        server: start a fabric sdk server"
    echo "        down: down fabric sdk server"
    echo "        bench: run fabric sdk bench"
}

function down_fabric_sdk_server() {
    echo "Down fabric sdk server ..."
    cd $CUR_DIR

    for node in ${FABRIC_SDK_IPs[@]}; do
        echo "==== down fabric-sdk on $node ===="
        ssh ${HOST_USER}@${node} "
            ps -ef | grep fabric-sdk | grep -v grep | awk '{print \$2}' | xargs kill -9 > /dev/null 2>&1
        " &
    done
    wait

    echo "Down fabric sdk server ... done"
}

function run_fabric_sdk_server() {
    echo "Run fabric sdk server ..."

    cd $CUR_DIR
    
    down_fabric_sdk_server

    local run_sdk_as_server="-s"

    # generate config yaml for fabric sdk
    for ((i=0; i<${ORG_NUM}; i++)); do
        echo "==== generate config yaml for org${i} ===="
        local port=$((i+8080))
        python3 ./gen_config_yaml/gen_sdk_config.py -o $ORDERED_NUM -p $ORG_NUM -i $((i+1))
    done

    # make fabric sdk
    cd ./fabric-sdk
    go build .

    # distribute fabric-sdk to all nodes & config hosts
    cd $CUR_DIR
    local org_id=1
    for node in ${FABRIC_SDK_IPs[@]}; do
        echo "==== send fabric-sdk binary & config to $node ===="
        ssh ${HOST_USER}@${node} "
            [ -d ${TARGET_HOST_FABRIC_SDK_DIR} ] && \
                rm -rf ${TARGET_HOST_FABRIC_SDK_DIR}/* || \
                mkdir -p ${TARGET_HOST_FABRIC_SDK_DIR}
            mkdir -p ${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin
        " && \
        scp ./hosts_config.sh ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
        scp ../../cluster_IP.conf ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
        scp ./gen_config_yaml/sdk-config-${org_id}.yaml ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
        scp ./fabric-sdk/fabric-sdk ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin/ > /dev/null 2>&1 && \
        scp -r ./crypto-config ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 &
        org_id=$((org_id+1))
    done
    wait

    # run fabric sdk
    local org_id=1
    for node in ${FABRIC_SDK_IPs[@]}; do
        echo "==== run fabric-sdk on $node ===="
        local KAFAKA_IP=${KAFKA_IPs[$((org_id-1))]}
        local KAFKA_PORT=$(($KAFKA_PORT_BASE+$org_id-1))
        # set -x
        ssh ${HOST_USER}@${node} "
            bash ${TARGET_HOST_FABRIC_SDK_DIR}/hosts_config.sh
            cd ${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin
            ./fabric-sdk -o $org_id $run_sdk_as_server \
                -c ../sdk-config-${org_id}.yaml \
                -b ${KAFAKA_IP}:${KAFKA_PORT} \
                > ./fabric-sdk-org${org_id}.log 2>&1 &
        " &
        # set +x
        org_id=$((org_id+1))
    done
    wait

    echo "Run fabric sdk server ... done"
}

grep_res() {
    cd $CUR_DIR

    local org_id=1
    rm -rf ./fabric-sdk-log && mkdir -p ./fabric-sdk-log
    # total_tps=0
    # total_throughput=0
    for node in ${FABRIC_SDK_IPs[@]}; do
        scp ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin/fabric-sdk-org${org_id}.log ./fabric-sdk-log/ > /dev/null 2>&1
        # tps=$(grep "TPS" ./fabric-sdk-log/fabric-sdk-org${org_id}.log | awk -F ' ' '{print $2}')
        throughput=$(grep "Throughput" ./fabric-sdk-log/fabric-sdk-org${org_id}.log | awk -F ' ' '{print $2}')
        echo "cluster [$((org_id-1))] throughput: $throughput MB/s"
        # total_tps=$(echo "$total_tps + $tps" | bc)
        # total_throughput=$(echo "$total_throughput + $throughput" | bc)
        # set +x
        org_id=$((org_id+1))
    done
    # printf "Total TPS: %.2f\n" "$total_tps"
    # printf "Total Throughput: %.2f MB/s\n" "$total_throughput"
}

run_fabric_sdk_bench() {
    echo "Run fabric sdk bench ..."

    # cd $CUR_DIR/fabric-sdk
    # # run fabric sdk bench
    # go build .
    # ./fabric-sdk -o 1 -b ${KAFKA_IPs[0]}:${KAFKA_PORT_BASE} -t ${KAFKA_TOPIC}  -c ../gen_config_yaml/sdk-config-1.yaml

    cd $CUR_DIR
    local run_sdk_as_server=""
    local obj_num=50000
    if [ $# -eq 1 ]; then
        obj_num=$1
    fi

    # generate config yaml for fabric sdk
    for ((i=0; i<${ORG_NUM}; i++)); do
        echo "==== generate config yaml for org${i} ===="
        local port=$((i+8080))
        python3 ./gen_config_yaml/gen_sdk_config.py -o $ORDERED_NUM -p $ORG_NUM -i $((i+1))
    done

    # make fabric sdk
    cd ./fabric-sdk
    go build .

    # distribute fabric-sdk to all nodes & config hosts
    cd $CUR_DIR
    local org_id=1
    for node in ${FABRIC_SDK_IPs[@]}; do
        echo "==== send fabric-sdk binary & config to $node ===="
        ssh ${HOST_USER}@${node} "
            [ -d ${TARGET_HOST_FABRIC_SDK_DIR} ] && \
                rm -rf ${TARGET_HOST_FABRIC_SDK_DIR}/* || \
                mkdir -p ${TARGET_HOST_FABRIC_SDK_DIR}
            mkdir -p ${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin
        " && \
        scp ./hosts_config.sh ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
        scp ../../cluster_IP.conf ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
        scp ./gen_config_yaml/sdk-config-${org_id}.yaml ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 && \
        scp ./fabric-sdk/fabric-sdk ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin/ > /dev/null 2>&1 && \
        scp -r ./crypto-config ${HOST_USER}@${node}:${TARGET_HOST_FABRIC_SDK_DIR}/ > /dev/null 2>&1 &
        org_id=$((org_id+1))
    done
    wait

    # run fabric sdk
    local org_id=1
    for node in ${FABRIC_SDK_IPs[@]}; do
        echo "==== run fabric-sdk on $node ===="
        local KAFAKA_IP=${KAFKA_IPs[$((org_id-1))]}
        local KAFKA_PORT=$(($KAFKA_PORT_BASE+$org_id-1))
        # set -x
        ssh ${HOST_USER}@${node} "
            bash ${TARGET_HOST_FABRIC_SDK_DIR}/hosts_config.sh
            cd ${TARGET_HOST_FABRIC_SDK_DIR}/fabric-sdk-bin
            ./fabric-sdk -o $org_id $run_sdk_as_server \
                -n $obj_num \
                -c ../sdk-config-${org_id}.yaml \
                -b ${KAFAKA_IP}:${KAFKA_PORT} \
                > ./fabric-sdk-org${org_id}.log 2>&1
        " &
        # set +x
        org_id=$((org_id+1))
    done
    wait

    grep_res

    echo "Run fabric sdk bench ... done"
}

# if [ $# -ne 1 ]; then
#     usage
#     exit -1
# fi

if [ $1 == "server" ]; then
    run_fabric_sdk_server
elif [ $1 == "bench" ]; then
    if [ $# -ne 2 ]; then
        run_fabric_sdk_bench
    else
        run_fabric_sdk_bench $2
    fi
elif [ $1 == "down" ]; then
    down_fabric_sdk_server
else
    usage
fi
